package com.jic.member.impl;

import java.util.Date;

import java.lang.Long;
import java.util.List;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.member.entity.MememberLabel;
import com.jic.member.mapper.MememberLabelMapper;
import com.jic.member.request.MememberLabelRequest;
import com.jic.member.service.MememberLabelService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员标签关联表
 *
 * @author : ZXL
 * @version : v1.0.0
 * @email :
 * @since : 2020-05-21 02:05:08
 */
@Service
public class MememberLabelServiceImpl implements MememberLabelService {

    @Resource
    private MememberLabelMapper mememberLabelMapper;


    @Override
    public long insert(MememberLabel mememberLabel) {
        long flag = 0;
        AdminOperate adminOperate = mememberLabel.getAdminOperate();
        MememberLabel instert = new MememberLabel();
        if (CollectionUtils.isNotEmpty(mememberLabel.getLabelIds())) {
            instert.setDeptType(adminOperate.getDeptType().getCode());
            instert.setDeptId(adminOperate.getDeptId());
            instert.setCreator(adminOperate.getOperator());
            instert.setDeleteFlag(0);
            instert.setCreateTime(new Date());
            instert.setCreatorName(adminOperate.getOperateName());
            List<Long> labelIds = mememberLabel.getLabelIds();
            List<Long> memberIds = mememberLabel.getMemberIds();
            MememberLabel query = new MememberLabel();
            query.setDeptId(adminOperate.getDeptId());

            for (int j = 0; j < memberIds.size(); j++) {
                query.setMemberId(memberIds.get(j));
                instert.setMemberId(memberIds.get(j));
                for (int i = 0; i < labelIds.size(); i++) {
                    //过滤重复数据
                    query.setDeleteFlag(0);
                    query.setLabelId(labelIds.get(i));
                    List<MememberLabel> rusultLabel = mememberLabelMapper.query(query);
                    if (CollectionUtils.isNotEmpty(rusultLabel)) {
                        continue;
                    }
                    instert.setLabelId(labelIds.get(i));
                    mememberLabelMapper.insert(instert);
                }
            }
        }
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mememberLabelMapper.delete(id);
    }

    @Override
    public long update(MememberLabel mememberLabel) {
        return mememberLabelMapper.update(mememberLabel);
    }

    @Override
    public MememberLabel queryByPrimaryKey(Long id) {
        return mememberLabelMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberLabel> query(MememberLabel mememberLabel) {
        return mememberLabelMapper.query(mememberLabel);
    }

    @Override
    public PageResult<MememberLabel> queryPage(MememberLabel mememberLabel, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        AdminOperate adminOperate = mememberLabel.getAdminOperate();
        mememberLabel.setDeptId(adminOperate.getDeptId());
        List<MememberLabel> list = mememberLabelMapper.queryPage(startIndex, pageQuery.getLimit(), mememberLabel);
        long count = mememberLabelMapper.queryPageCount(mememberLabel);
        list.forEach(item -> {
            //查询当前标签的标记次数
            MememberLabel query = new MememberLabel();
            query.setDeptId(item.getDeptId());
            query.setLabelId(item.getLabelId());
            query.setDeleteFlag(0);
            Long markTimes = mememberLabelMapper.queryPageCount(query);
            item.setMarkTimes(markTimes);
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public Long deleteInfo(MememberLabelRequest form) {
        //根据删除memberId和deptId
        if (null != form) {
            mememberLabelMapper.deleteLabel(form.getMemberId(), form.getAdminOperate().getDeptId());
        }
        return null;
    }
}
